//
//  ZHFPSLabel.m
//  ZYT_IOS
//
//  Created by 正合适 on 2018/1/18.
//  Copyright © 2018年 正合适. All rights reserved.
//

#import "ZHFPSLabel.h"


#define kScreenW  [UIScreen mainScreen].bounds.size.width
#define kScreenH  [UIScreen mainScreen].bounds.size.height

@interface ZHFPSLabel ()

/**linker*/
@property (nonatomic, strong) CADisplayLink *liner;
/**计数*/
@property (nonatomic, assign) NSUInteger count;
/**上次的时间*/
@property (nonatomic, assign) NSTimeInterval lastTime;

@end

@implementation ZHFPSLabel

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
        [self initThread];
        [self addGesture];
    }
    return self;
}

- (void)createUI {
    self.layer.cornerRadius = 8;
    self.layer.masksToBounds = YES;
    self.textColor = [UIColor whiteColor];
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    self.textAlignment = NSTextAlignmentCenter;
    self.font = [UIFont systemFontOfSize:14];
    self.userInteractionEnabled = YES;
}

- (void)initThread {
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(thread) object:nil];
    [thread start];
}

- (void)addGesture {
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
    [self addGestureRecognizer:panGesture];
}

- (void)thread {
    _liner = [CADisplayLink displayLinkWithTarget:self selector:@selector(linkerAction:)];
    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
    [_liner addToRunLoop:currentRunLoop forMode:NSRunLoopCommonModes];
    [currentRunLoop run];
}

- (void)linkerAction:(CADisplayLink *)link {
    if (link.timestamp == 0) {
        _lastTime = link.timestamp;
        return;
    }
    _count++;
    NSTimeInterval time = link.timestamp - _lastTime;
    if (time<1) {
        return;
    }
    _lastTime = link.timestamp;
    float fps = _count/time;
    _count = 0;
    NSString *str = [NSString stringWithFormat:@"FPS:%d",(int)round(fps)];
    NSMutableAttributedString *muStr = [[NSMutableAttributedString alloc] initWithString:str];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.attributedText = muStr;
    });
}

- (void)panGestureAction:(UIPanGestureRecognizer *)recognizer {
    //返回在横坐标上、纵坐标上拖动了多少像素
    CGPoint point=[recognizer translationInView:self];
    CGFloat centerX = recognizer.view.center.x+point.x;
    CGFloat centerY = recognizer.view.center.y+point.y;
    CGFloat viewHalfH = recognizer.view.frame.size.height/2.0f;
    CGFloat viewhalfW = recognizer.view.frame.size.width/2.0f;
    //确定特殊的centerY
    if (centerY - viewHalfH < 0) {
        centerY = viewHalfH;
    }
    if (centerY + viewHalfH > kScreenH ) {
        centerY = kScreenH - viewHalfH;
    }
    // 确定特殊的centerX
    if (centerX - viewhalfW < 0){
        centerX = viewhalfW;
    }
    if (centerX + viewhalfW > kScreenW){
        centerX = kScreenW - viewhalfW;
    }
    recognizer.view.center=CGPointMake(centerX, centerY);
    // 拖动完之后，每次都要用setTranslation:方法制0这样才不至于不受控制般滑动出视图
    [recognizer setTranslation:CGPointMake(0, 0) inView:self];
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        // 拖动结束后，判断是否超出屏幕
        [self changePosition];
    }
}

- (void)changePosition {
    if (self.frame.origin.x<=kScreenW/2) {
        [self animationWithLeft:YES];
    } else {
        [self animationWithLeft:NO];
    }
}

- (void)animationWithLeft:(BOOL)bLeft {
    float toValue = 0.0f;
    if (!bLeft) {
        toValue = kScreenW-self.frame.size.width;
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(toValue, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
    }];
}

- (void)dealloc {
    [_liner invalidate];
    _liner = nil;
    NSLog(@"----- FPSLabel被移除 -----");
}
@end
